#include "lm_dialog.h"
#include "../common/lm_mem.h"


static bool lm_dialog_keyboard_handler(lm_query_win_t *win, char *edit_ret, int text_len)
{
    bool btn_ret = false;
    int kb_input;

    char *pedit = edit_ret + text_len;

    while(1) {
        kb_input = lm_get_kb_value();

        if(kb_input != KEYBOARD_INVALID)
        {
            if(kb_input == KEYBOARD_ENTER) {
                break;
            }
            else if(kb_input == KEYBOARD_RIGHT) {
                btn_ret = false;
                lm_query_win_set_status(win, false);
            }
            else if(kb_input == KEYBOARD_LEFT) {
                btn_ret = true;
                lm_query_win_set_status(win, true);
            }
            else if((kb_input >= '0' && kb_input<='9') 
                    || (kb_input >= 'A' && kb_input<='Z')
                    || (kb_input >= 'a' && kb_input<='z')
                    || kb_input =='/' || kb_input == '\\' || kb_input == '-' || kb_input == '*' || kb_input == ':'
                    || kb_input == '_' || kb_input == '+' || kb_input == '.' || kb_input == '\''
                ) {
                *pedit = kb_input;
                pedit++;
                *pedit = 0;
                lm_query_win_set_text(win, edit_ret);
            }
            else if(kb_input == 8) { //backspace key
                if(pedit != edit_ret) {
                    pedit --;
                    *pedit = 0;
                    lm_query_win_set_text(win, edit_ret);
                }
            }
        }
    }

    return btn_ret;
}

const char* lm_edit_dialog_create(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const char *title, const char *text, lm_color_t color)
{
    bool btn_ret = false;

    char *edit_ret = lm_malloc(4096);
    if(edit_ret == NULL) {
        return NULL;
    }

    memset(edit_ret, 0, 4096);
    memcpy(edit_ret, text, strlen(text)+1);

    lm_query_win_t *query = lm_query_win_create(NULL, x, y, width, height, title, false, color);
                                            
    if(query == NULL) {
        return NULL;
    }

    lm_query_win_set_text(query, text);

    btn_ret = lm_dialog_keyboard_handler(query, edit_ret, strlen(text));

    curs_set(0);
    
    lm_query_win_delete(query);

    if(btn_ret) {
        return edit_ret;
    }
    else
        return NULL;
}



static bool lm_query_keyboard_handler(lm_query_win_t *win)
{
    bool btn_ret = false;
    int kb_input;

    while(1) {
        kb_input = lm_get_kb_value();

        if(kb_input !=KEYBOARD_INVALID)
        {
            if(kb_input == KEYBOARD_ENTER) {
                break;
            }
            else if(kb_input == KEYBOARD_RIGHT) {
                btn_ret = false;
                lm_query_win_set_status(win, false);
            }
            else if(kb_input == KEYBOARD_LEFT) {
                btn_ret = true;
                lm_query_win_set_status(win, true);
            }
            else if(kb_input == KEYBOARD_KEY_N || kb_input == KEYBOARD_KEY_n) {
                btn_ret = false;
                lm_query_win_set_status(win, false);
                break;
            }
            else if(kb_input == KEYBOARD_KEY_Y || kb_input == KEYBOARD_KEY_y) {
                btn_ret = true;
                lm_query_win_set_status(win, true);
                break;
            }
        }
    }

    return btn_ret;
}

bool lm_query_dialog_create(uint16_t width, uint16_t height, const char *title, const char *text, lm_color_t color)
{
    bool btn_ret = false;

    lm_query_win_t *query = lm_query_win_create(NULL, (COLS - width)/2, (LINES - height)/2, 
                                                width, height, title, false, color);
                                            
    if(query == NULL) {
        return NULL;
    }

    lm_query_win_set_text(query, text);

    btn_ret = lm_query_keyboard_handler(query);

    curs_set(0);
    
    lm_query_win_delete(query);

    return btn_ret;
}

bool lm_error_dialog_create(uint16_t width, uint16_t height, const char *text)
{
    lm_color_t color = {
        .rgb_bg_index = 391,
        .rgb_fg_index = 256,
    };

    return lm_query_dialog_create(width, height, "ERROR", text, color);
}


bool lm_warning_dialog_create(uint16_t width, uint16_t height, const char *text)
{
    lm_color_t color = {
        .rgb_bg_index = 286,
        .rgb_fg_index = 256,
    };

    return lm_query_dialog_create(width, height, "WARRING", text, color);
}


bool lm_information_dialog_create(uint16_t width, uint16_t height, const char *text)
{
    lm_color_t color = {
        .rgb_bg_index = 312,
        .rgb_fg_index = 256,
    };

    return lm_query_dialog_create(width, height, "INFORMATION", text, color);
}
